var _api = _interopRequireDefault(require("../../../../utils/api")), _util2 = _interopRequireDefault(require("../../../../utils/util")), _contactUtil = require("../../../../utils/contact-util"), _util3 = _interopRequireDefault(require("../../../../public/utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = new Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
}

var sortList = [ {
    text: "按部门",
    type: "corpnameLetter",
    orderBy: "asc",
    sort: 2
}, {
    text: "按名称",
    type: "realnameLetter",
    orderBy: "desc",
    sort: 1
} ], LIMIT = 50;

Component({
    data: {
        emptyTip: "你还没有名片哦，先去创建名片再使用通讯录吧...",
        sortIndex: "0",
        sortList: sortList.map(function(t) {
            return "".concat(t.text, "排序");
        }),
        GRADE: _contactUtil.GRADE,
        CLAIM: _contactUtil.CLAIM,
        search: {}
    },
    ready: function() {
        this.onLoad();
    },
    methods: {
        onLoad: function() {
            var t = this.$appData, e = t.contact, a = t.card;
            this.setCardData(a), this.setContactData(e), this.initEvent();
        },
        initEvent: function() {
            var e = this;
            [ [ "card.change", this.setCardData.bind(this) ], [ "contact.change", this.setContactData.bind(this) ], [ "contact.user.change", function() {
                return e.loadList(!0);
            } ], [ "contact.origanization.update", function() {
                return e.loadList(!0);
            } ] ].forEach(function(t) {
                return e.$addListener.apply(e, _toConsumableArray(t));
            });
        },
        onPullDownRefresh: function() {
            (0, _contactUtil.loadContact)().then(wx.stopPullDownRefresh);
        },
        onReachBottom: function() {
            !this.data.search.inputShowed && this.hasMore && this.loadList();
        },
        onShow: function() {
            this.createCliam && (this.createCliam = !1, wx.showModal({
                content: "名片已发出，对方收到后即可交换名片",
                showCancel: !1
            }));
        },
        onSearch: function(t) {
            var n = this;
            clearTimeout(this.timer), this.timer = setTimeout(function() {
                n.setData({
                    "search.loading": !0,
                    "search.keyword": t
                }), _api.default.minicard.contact.searchmember({
                    data: {
                        keyword: t
                    },
                    complete: function() {
                        n.setData({
                            "search.loading": !1
                        });
                    }
                }).then(function(t) {
                    var e = t.data, a = e.count, r = e.list;
                    n.setData({
                        "search.list": r,
                        "search.count": a
                    });
                });
            }, 700);
        },
        tiggerSearch: function(t) {
            var e = {
                "search.inputShowed": t
            };
            t || (e["search.count"] = 0, e["search.list"] = [], e["search.keyword"] = ""), this.setData(e);
        },
        setContactData: function(t) {
            var e = t.contact, a = t.total;
            this.setData({
                contact: e,
                total: a
            }), this.loadRequest();
        },
        setCardData: function(t) {
            this.setData({
                card: t
            });
        },
        loadRequest: function() {
            var t = this.data, e = t.contact, a = t.search.keyword;
            e && e.guid && (a ? this.onSearch(a) : this.loadList(!0));
        },
        resetData: function() {
            this.offset = 0, this.hasMore = !0;
        },
        loadList: function(a) {
            var r = this;
            a && this.resetData();
            var t = this.data, e = t.sortIndex, n = t.contact, i = sortList[e], o = i.type, s = i.sort;
            return _api.default.minicard.contact.lists({
                data: {
                    sort: s,
                    offset: this.offset,
                    order: 0,
                    guid: n.guid,
                    limit: LIMIT
                }
            }).then(function(t) {
                var e = t.data.list;
                return r.handleLoadListResult(e, o, a);
            });
        },
        handleLoadListResult: function(t, e, a) {
            var r = this["groupBy".concat(_util3.default.ucFirst(e))](t);
            if ("realnameLetter" === e && _util2.default.sortByLetterKey(r.groupedKeys), a) this.setData(r); else {
                var n = _util2.default.concatListToData(r, this.data, !0);
                this.setData(n);
            }
            this.offset += LIMIT, this.hasMore = LIMIT <= t.length;
        },
        groupByRealnameLetter: function(t) {
            return t = t.map(function(t) {
                return t.realnameLetter = t.realnameLetter || "#", t;
            }), _util3.default.groupByLetterKey(t, "realnameletter");
        },
        groupByCorpnameLetter: function(t) {
            return t = t.map(function(t) {
                return t.name = (t.name || "").replace(/^>/, "") || "#", t;
            }), _util3.default.groupByLetterKey(t, "name");
        },
        gotoRoute: function(t) {
            var e = t.currentTarget.dataset.url;
            wx.navigateTo({
                url: e
            });
        },
        call: function(t) {
            var e = t.currentTarget.dataset.mobile;
            wx.makePhoneCall({
                phoneNumber: e
            });
        },
        selectSortBy: function(t) {
            var e = t.detail.value;
            this.data.sortIndex !== e && (this.setData({
                sortIndex: e
            }), this.loadList(!0));
        },
        reportShare: function() {
            this.$report("contact_share");
        },
        onShareAppMessage: function() {
            var t = this.data.card;
            return !t || t.waitJoin ? {
                title: "欢迎使用小名片",
                path: "/pages/tabbar/card/card"
            } : (0, _contactUtil.inviteShareConfig)({
                params: {
                    guid: this.data.contact.guid
                }
            });
        }
    }
});